package com.lbz.controller.backend;

import com.github.pagehelper.PageInfo;
import com.lbz.common.Constant;
import com.lbz.common.ResponseCode;
import com.lbz.common.ServerResponse;
import com.lbz.entity.User;
import com.lbz.service.OrderService;
import com.lbz.service.UserService;
import com.lbz.vo.OrderVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * @Author 林必昭
 * @Date 2019/6/24 23:45
 */

@Controller
@RequestMapping("/manage/order/")
public class OrderManageController {

    @Autowired
    private UserService userService;

    @Autowired
    private OrderService orderService;

    /**
     * 后台订单列表/查询所有订单
     * @param session
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping("list.do")
    @ResponseBody
    public ServerResponse<PageInfo> orderList(HttpSession session,
                                         @RequestParam(value = "pageNum",defaultValue = "1") int pageNum,
                                         @RequestParam(value = "pageSize",defaultValue = "10") int pageSize){
        User user = (User) session.getAttribute(Constant.CURRENT_USER);
        if (user==null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登录，请以管理员的身份登陆");
        }
        if (userService.checkRoleAdmin(user).isSuccess()){
            return orderService.manageList(pageNum,pageSize);
        }
        return ServerResponse.createByErrorMessage("无权限操作");
    }

    /**
     * 后台查看订单详情
     * @param session
     * @param orderNo
     * @return
     */
    @RequestMapping("detail.do")
    @ResponseBody
    public ServerResponse<OrderVO> orderDetail(HttpSession session,Long orderNo){

        User user = (User) session.getAttribute(Constant.CURRENT_USER);
        if (user==null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登录，请以管理员的身份登陆");
        }
        if (userService.checkRoleAdmin(user).isSuccess()){
            return orderService.manageDetail(orderNo);
        }
        return ServerResponse.createByErrorMessage("无权限操作");
    }

    /**
     * 后台根据订单号产查询
     * @param session
     * @param orderNo
     * @param pageNum
     * @param pageSize
     * @return
     */
    //todo 查询出来的分页信息显示不正确
    @RequestMapping("search.do")
    @ResponseBody
    public ServerResponse<PageInfo> orderSearch(HttpSession session,Long orderNo,
                                          @RequestParam(value = "pageNum",defaultValue = "1") int pageNum,
                                          @RequestParam(value = "pageSize",defaultValue = "10") int pageSize){

        User user = (User) session.getAttribute(Constant.CURRENT_USER);
        if (user==null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登录，请以管理员的身份登陆");
        }
        if (userService.checkRoleAdmin(user).isSuccess()){
            return orderService.manageSearch(orderNo,pageNum,pageSize);
        }
        return ServerResponse.createByErrorMessage("无权限操作");
    }

    /**
     * 发货接口
     * @param session
     * @param orderNo
     * @return
     */
    @RequestMapping("send_goods.do")
    @ResponseBody
    public ServerResponse<String> orderSendGoods(HttpSession session,Long orderNo){

        User user = (User) session.getAttribute(Constant.CURRENT_USER);
        if (user==null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登录，请以管理员的身份登陆");
        }
        if (userService.checkRoleAdmin(user).isSuccess()){
            return orderService.manageSendGoods(orderNo);
        }
        return ServerResponse.createByErrorMessage("无权限操作");
    }
}
